<template>
    <div class="home_Blog">
        <div class="blog" v-for="(value,index) in blogs" :key="index">
            <router-link :to="{path:`/post`,query:{'post':JSON.stringify(value)}}" ><h3>{{value.title}}</h3></router-link>
            <p class="blogContent">{{value.content | getP}}</p>
            <span class="readCount">{{value.readsCount}}次阅读</span>
            <span class="postTime">{{value.postDate | formateDate}}</span>
        </div>
    </div>
</template>

<script>
export default {
    name:'HomeMain',
    props:['userLogin','userChoosed'],
    data() {
      return {
        blogs:[],
      };
    },
    filters:{
        getP:function(value){
            let div=document.createElement('div');
            div.innerHTML=value;
            let pArr=div.getElementsByTagName('p');
            let text='';
            for(let i=0;i<pArr.length;i++){
                if(text.length>100)
                    break;
                if(pArr[i].innerText!='&nbsp;')
                    text+=pArr[i].innerText;
            }
            return text;
        },
        
    },
    methods: {
        getBlogs(userId){
            this.$axios.post('/api/posts/getUserPosts',{
                user:userId,
            })
            .then(res=>{
                for(let i=0;i<res.data.data.length;i++){
                    res.data.data[i].author=this.userChoosed;
                }
                this.blogs=res.data.data; 
            })
        }
    },
    created(){
        // if(this.userChoosed.id){
        //     this.getBlogs(this.userChoosed.id);
        // }
    },
    watch:{
        userChoosed:function(){
            this.getBlogs(this.userChoosed.id);
        },
    }
}
</script>

<style scoped>
.home_Blog{
    width:100%;
    /* height:100px; */
    box-shadow: 0 1px 2px 0 rgba(0,0,0,.04);
    border:1px solid #ccc;
    padding:5px 5px 10px 5px;
    box-sizing: border-box;
}
.blog{
    padding:15px;
    box-sizing: border-box;
}
.blogContent{
    font-size: 14px;
    color: #858585;
    line-height: 24px;
    display: -webkit-box;
    -webkit-box-orient: vertical;
    -webkit-line-clamp: 2;
    text-overflow: ellipsis;
    margin-top:10px;
    margin-bottom:10px;
    text-indent: -3px;
}
.readCount{
    font-size:10px;
    color:#858585;
    float:left;
    /* margin-left:15px; */
}
.postTime{
    font-size:10px;
    color:#858585;
    float:right;
    margin-right:20px;
}
a{
    text-decoration: none;
    color:#000;
}
</style>

